% function krnl = getCosKernel(y)
%     % Normalize rows of the matrix
%     norm_y = sqrt(sum(y.^2, 2));
%     y_normalized = y ./ norm_y;
% 
%     % Calculate cosine similarity between rows
%     cosine_similarity_matrix = y_normalized * y_normalized';
% 
%     % Exponentiate the similarity matrix to create the kernel
%     gamma = 0.1; % You can adjust this parameter
%     krnl = exp(-gamma * (1 - cosine_similarity_matrix));
% end

function cosine_similarity_matrix = getCosKernel(y)
    krnl = y * y';
    y = krnl / mean(diag(krnl));

    % 计算每一行的范数（模长）
    norm_y = sqrt(sum(y.^2, 2));

    % 计算余弦相似度核矩阵
    cosine_similarity_matrix = zeros(size(y, 1));
    for i = 1:size(y, 1)
        if norm_y(i) > 0  % 检查基因关联向量是否为零向量
            for j = i:size(y, 1)
                if norm_y(j) > 0  % 检查基因关联向量是否为零向量
                    cosine_similarity_matrix(i, j) = dot(y(i, :), y(j, :)) / (norm_y(i) * norm_y(j));
                    cosine_similarity_matrix(j, i) = cosine_similarity_matrix(i, j);
                end
            end
        end
    end
end


% function cosine_similarity_matrix=getCosKernel(y)
% cosine_similarity_matrix = zeros(size(y, 1)); % 初始化余弦相似度核矩阵
% 
% for i = 1:size(y, 1)
%     for j = i+1:size(y, 1)
%         dot_product = sum(y(i, :) .* y(j, :));
%         magnitude_i = norm(y(i, :));
%         magnitude_j = norm(y(j, :));
%         cosine_similarity = dot_product / (magnitude_i * magnitude_j);
%         cosine_similarity_matrix(i, j) = cosine_similarity;
%         cosine_similarity_matrix(j, i) = cosine_similarity; % 对称性
%     end
% end
% end